<!--
 * @Description: 附件管理
 * @Author: 
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-24 14:26:45
-->
<template>
  <div class="pages_content table attachment">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display: flex;">
        <div class="main_left">
          <div style="width: 200px;padding: 20px;">
            <el-tree default-expand-all :data="treearr" class="tree-line"  :indent="0"  :props="defaultProps" :expand-on-click-node="false" @node-click="setTree">
              <span slot-scope="{ node, data }" class="custom-tree-node">
                <icon :name="data.icon" :w="22" :h="14" style="padding: 0 4px;"></icon>
                <span>{{ node.label }}</span>
              </span>
            </el-tree>
          </div>
        </div>
        <div class="main_right">
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px;">名称</span>
                  <el-input v-model="tableParam.name" style="width: 200px;" clearable placeholder="请输入" />
                </span>
                <span style="margin-left: 24px;">
                  <span style="margin-right: 12px;">编码</span>
                  <el-input v-model="tableParam.code" style="width: 200px;" clearable placeholder="请输入" />
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), queryPage()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" @click="reset()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="add()">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                  添加
                </el-button>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='SHANCHU'))!==-1" size="small" class="bottom_two" @click="batchUpdateOrDelete('update_status', 0)">
                  <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                  删除
                </el-button>
              </div>
              <div>
                <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="$Download(DownloadURL, tableParam)">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_export1"></i>
                  </span>
                  导出
                </el-link>
              </div>
            </div>
            <!-- 列表 -->
            <div class="card_info" :style="style">
              <el-table ref="multipleTable" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="50"></el-table-column>
                <el-table-column align="center" label="编码" prop="code" min-width="140"></el-table-column>
                <el-table-column align="center" label="名称" prop="name" min-width="140"></el-table-column>
                <el-table-column align="center" label="删除" min-width="80">
                  <template slot-scope="scope">
                    {{scope.row.deleteLimit*1===1?'是':'否'}}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="下载" min-width="80">
                  <template slot-scope="scope">
                    {{scope.row.downloadLimit*1===1?'是':'否'}}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="水印" min-width="80">
                  <template slot-scope="scope">
                    {{scope.row.watermark*1===1?'是':'否'}}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="系统名称" prop="systemName" min-width="140"></el-table-column>
                <el-table-column align="center" label="备注" prop="remark" min-width="200"></el-table-column>
                <el-table-column align="center" label="路径" prop="path" min-width="140"></el-table-column>
                <el-table-column align="center" label="操作" min-width="120" fixed="right">
                  <template slot-scope="scope">
                    <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="update(scope.row)">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div class="card_bottom">
              <div class="card_bottom_left">
                <div class="top_page_totle">总共{{ total }}条</div>
                <el-pagination :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total"></el-pagination>
              </div>
              <div>
                <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="queryPage()">刷新</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="80%" :before-close="resefrom" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">{{ dialogtitle }}</span>
      <div class="dialoginfo">
        <el-form ref="dialogForm" :rules="rules" :inline="true" :model="from" label-width="90px">
          <el-form-item prop="code">
            <span slot="label" style="color:  #FF0000 ;">编码</span>
            <el-input v-model="from.code" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="name">
            <span slot="label" style="color:  #FF0000 ;">名称</span>
            <el-input v-model="from.name" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="systemId">
            <span slot="label">系统名称</span>
            <el-select v-model="from.systemId" placeholder="请选择">
              <el-option v-for="item in treearr" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="路径" prop="path">
            <el-input v-model="from.path" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item label="控制删除权限" prop="deleteLimit" label-width="110px">
            <el-radio-group v-model="from.deleteLimit">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="控制下载权限" prop="downloadLimit" label-width="110px">
            <el-radio-group v-model="from.downloadLimit">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="水印" prop="watermark" label-width="110px">
            <el-radio-group v-model="from.watermark">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="系统描述" prop="remark">
            <el-input v-model="from.remark" placeholder="请输入内容" class="textarea" type="textarea" resize="none" rows="3"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button size="mini" class="bottom_all" style="margin-left: 0;" @click="addItemData()">
          <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
          添加
        </el-button>
      </div>
      <el-table :data="from.items" style="width: 100%">
        <el-table-column label="附件编码" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.code"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="附件名称" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="允许多个" width="120" align="center">
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.multiLimit" style="display: flex;">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column label="列数" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.columnCount"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="大小(M)" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.trillionSize"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="允许文件类型" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.fileType"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="扩展配置" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.extConfig"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="扩展配置" align="center">
          <template slot-scope="scope">
            <el-button size="mini" class="bottom_two" @click="delItemData(scope.$index)">
              <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom()">取 消</el-button>
        <el-button class="bottom_all" @click="save()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import breaDcrumb from "@/components/breadcrumb";
import { mapGetters } from "vuex";
import {
  queryPage,
  saveOrUpdate,
  getDetail,
  batchUpdateOrDelete, Download,
} from "@/api/basedata/attachment.js";
import { queryPage as querybusinessPage } from "@/api/basedata/business.js";

export default {
  components: {
    breaDcrumb,
  },
  data() {
    return {
      DownloadURL: Download,
      defaultProps: {
        children: "children",
        label: "name",
      },
      /*添加修改弹窗*/
      dialogVisible: false,
      dialogtitle: "添加附件",
      /*弹要修改过着添加的数据*/
      from: {
        name: "",
        code: "",
        deleteLimit: 1,
        downloadLimit: 1,
        watermark: 1,
        systemId: "",
        remark: "",
        path: "",
        items: [],
      },
      /*判断收起展开*/
      showCardNav: true,
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
        systemId: [
          { required: true, message: "请选择系统", trigger: "change" },
        ],
      },
      /*列表选中的数据集*/
      tableParam: {
        page: 1,
        pageSize: 12,
        code: "",
        name: "",
        systemId: "",
      },
      total: 0,
      ids: [],
      sortIds: [],
      tableData: [],
      treearr: [],
      oldlist: [],
      ButtonList: []
    };
  },
  beforeCreate() {
    let params = {
      page: 1,
      pageSize: 10000000,
    };
    querybusinessPage(params).then((res) => {
      this.treearr = res.data.data.map((item) => {
        item.icon = "tree_icon_folder_open";
        return item;
      });
    });
  },
  mounted() {
    this.ButtonList = this.$ButtonList()
    this.queryPage();
  },
  methods: {
    /*弹窗列表数据添加*/
    addItemData() {
      const data = {
        code: "",
        extConfig: "",
        fileType: "",
        name: "",
        trillionSize: "",
        columnCount: "",
        multiLimit: 1,
      };
      this.from.items.push(data);
    },
    delItemData(index) {
      this.from.items.splice(index, 1);
    },
    /*重置*/
    reset() {
      this.tableParam.code = "";
      this.tableParam.name = "";
      this.queryPage();
    },
    /*删除，禁用，启用，排序*/
    batchUpdateOrDelete(type, status) {
      if (this.ids.length == 0) {
        this.$message({
          message: "请选择要操作的数据项",
          type: "warning",
        });
        return;
      }
      if (type === "update_status") {
        this.$confirm("此操作将永久删除该信息?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          cancelButtonClass: "bottom_all_two",
          confirmButtonClass: "bottom_all",
          type: "warning",
        })
          .then(() => {
            batchUpdateOrDelete({
              ids: this.ids,
              status: status,
              type: type,
            }).then(() => {
              this.reset();
            });
          })
          .catch(() => { });
      } else {
        batchUpdateOrDelete({
          ids: this.ids,
          status: status,
          type: type,
        }).then(() => {
          this.queryPage();
        });
      }
    },
    /*接受树状数据*/
    setTree(e) {
      this.tableParam.systemId = e.id;
      this.tableParam.page = 1;
      this.queryPage();
    },
    /*添加*/
    add() {
      this.from = this.$options.data().from
      this.from.items = []
      this.dialogVisible = true;
      setTimeout(() => {
        this.$refs.dialogForm.clearValidate();
      }, 10)
      this.dialogtitle = "添加附件";
    },
    /*修改*/
    update(data) {
      getDetail({ id: data.id }).then((res) => {
        this.oldlist = JSON.parse(JSON.stringify(res.data.items));
        this.from = res.data;
        this.dialogtitle = "更新功能";
        this.dialogVisible = true;
        setTimeout(() => {
          this.$refs.dialogForm.clearValidate();
        }, 10)
      });
    },
    /*获取列表*/
    queryPage() {
      queryPage(this.tableParam).then((res) => {
        this.total = res.data.totalRows;
        this.tableData = res.data.data;
      });
    },
    /*保存或修改*/
    save() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          let allHasData = this.from.items.filter((item) =>
            this.oldlist.some((items) => item.id == items.id)
          );
          let deleteData = this.oldlist.filter(
            (item) => !allHasData.some((items) => item.id == items.id)
          );
          let addData = this.from.items.filter(
            (item) => !allHasData.some((items) => item.id == items.id)
          );
          deleteData.forEach((res) => {
            res.status = 0;
          });
          this.from.items = [...allHasData, ...addData, ...deleteData];
          saveOrUpdate(this.from).then((res) => {
            if (res.code === 1001) {
              this.$message({
                message: '保存成功',
                type: "success",
              });
              this.resefrom();
              this.queryPage();
            }
          });
        }
      });
    },
    resefrom() {
      this.dialogVisible = false;
    },
    /*表头复选框*/
    handleSelectionChange(val) {
      let _this = this;
      _this.ids = [];
      _this.sortIds = [];
      val.forEach((item) => {
        _this.ids.push(item.id);
        _this.sortIds.push({ id: item.id, sortNum: item.sortNum });
      });
    },
  },
  computed: {
    ...mapGetters({
      screenWidth: "getscreenWidth",
      screenHeight: "getscreenHeight",
      Collapse: "getsCollapse",
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 324;
      } else {
        num = this.screenWidth - 421;
      }
      return "width:" + num + "px;";
    },
  },
};
</script>
<style lang="scss" scoped>
.attachment {
  .dialoginfo {
    margin-bottom: 20px;
    .el-input,
    .el-select {
      width: 230px;
    }
    .textarea_input {
      padding-top: 10px;
      align-items: flex-start;
      height: 70px;
    }
    .textarea {
      width: 610px;
    }
  }
}
</style>
